package jchat.genral;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import jchat.model.person;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.Toast;


public class ReadContact  {
	Context context;
	ArrayList<person> arr=new ArrayList<person>();
	person objperson;
	String name,phone;
	StringBuilder xml;
	public ReadContact(Context context) {
		
		this.context=context;
		// TODO Auto-generated constructor stub
	}

	public JSONObject getcontects()
	{
		JSONObject objsubmit=null;
		JSONObject objresult=null;
		try
		{
		
		ContentResolver cr =context.getContentResolver();
	   	 Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
	               null, null, null, null);
	       
	        if (cur.getCount() > 0) {   
	       	while (cur.moveToNext()) {
			        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
			         name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
			        	System.out.println("name : " + name + ", ID : " + id);
			        	//Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
			        	//contactView.setText(name);
			        	// get the phone number
			        	Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
			         		    			   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
			         		    			   new String[]{id}, null);
	        	        while (pCur.moveToNext()) {
	        	        	  phone = pCur.getString(
	        	        			 pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
	        	        	  System.out.println("phone" + phone);
	        	        	 // Toast.makeText(getApplicationContext(), phone, Toast.LENGTH_LONG).show();
	        	        	  
	        	        } 
	        	        arr.add(new person(name, phone));
	        	       
	        	//       int lenght=cur.getCount();
	        	       
	        	       
			        }
			       
			        }
	       
		   }
	        int lenght=arr.size();
	       
	       /* xml=new StringBuilder();
	        xml.append("<Friends>");
        	for (person item : arr) {
				xml.append("<Name>"+item.getname()+"</Name>");
				xml.append("<Mobile>"+item.getmobile_no().substring(3)+"</Mobile>");
				
			}
        	xml.append("</Friends>");*/
	        JSONArray arrya=new JSONArray();
	        //arr.get(lenght).getmobile_no();
	      for(int i=0 ;i<lenght;i++)
	       {
	       
	        try {
	        	objsubmit=new JSONObject();
	        	objsubmit.put("Name",arr.get(i).getname() );
	        	objsubmit.put("Mobile", arr.get(i).getmobile_no().substring(3));
	        	
				/*xml.append("<Friends>");
				xml.append("<Name>"+arr.get(i).getname()+"</Name>");
				xml.append("<Mobile>"+arr.get(i).getmobile_no()+"</Mobile>");
				xml.append("</Friends>");
	        	/*xml="<Friends>"+
				                "<Name>"+arr.get(i).getname()+"</Name>"+
						             "<Mobile>"+arr.get(i).getmobile_no()+"</Mobile>" +
						             		"</Friends>";*/
				 
				 
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        arrya.put(objsubmit);
	       }
	      
	      try {
			objresult=new JSONObject();
			objresult.put("Contact", arrya);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      
	}
	catch(Exception e)
	{
		Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
	}
	    
		return objresult;
	    
	}

}
